// pages/paycost/paycost.js
var Ajax = require('../../utils/Ajax.js')
var utils = require('../../utils/util.js');
var Api = require('../../utils/Api.js');
var config = require('../../base.conf.js').config;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusType: 'none', //数据查询错误并且数组为空 true为错误 false正常
    statuserro: false, //数据是否为空 value:empty
    moreType: 'noMore',
    currentPage:1,
    nogoodsH:0,
    pageSize:20,
    recordlist: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    utils.showLoading();
    const sysInfo = wx.getSystemInfoSync();
    const screenWidth = sysInfo.screenWidth;
    this.windowHeight = sysInfo.windowHeight;
    this.factor = screenWidth / 750;
    this.setData({
      nogoodsH: (this.windowHeight / this.factor) - 130,
    }, () => {
      this.refresh();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  refresh() { //下拉刷新
    var {
      moreType,
      currentPage,
    } = this.data;
    if (moreType != "loading") {
      this.setData({
        currentPage: 1,
        moreType: 'none'
      }, () => {
        this.paycost();
      })
    }
  },
  oncallPhone() {
    wx.makePhoneCall({
      phoneNumber: '4001668298',
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  paycost() { //查询厂牌商品列表
    var {
      options,
      moreType,
      pageSize,
      currentPage,
      nogoodsH
    } = this.data;
    Ajax.post("lifeServiceTrade", {
       userId: app.getValueSync("openid"),
      pageNum: currentPage,
      pageSize,
    }).then((d) => {
      utils.hideLoading();
      wx.stopPullDownRefresh();
      if (d.status == 200 && d.data) {
        moreType = "showText";
        var recordlist = d.data.list || [];
        recordlist.map((item)=>{
          var timer = new Date(item.updateTime);
          item.times = utils.format(timer, "yyyy-MM-dd hh:mm:ss");//毫秒转换
        })
        recordlist.length < pageSize ? moreType = "noMore" : moreType
        if (currentPage != 1) {
          recordlist = this.data.recordlist.concat(recordlist);
        }
        var listHeight = recordlist.length * 124+100;
        if (listHeight > nogoodsH){
          listHeight = nogoodsH
        }
        this.setData({
          recordlist: recordlist,
          scoorh: listHeight,
          statusType: recordlist.length > 0 ? 'none' : 'empty',
          statuserro: false,
          moreType
        })
      } else {
        var { recordlist } = this.data;
        this.setData({
          moreType: "none",
          statusType: recordlist.length > 0 ? 'none' : 'empty',
          statuserro: recordlist.length > 0 ? false : true,
        })
        utils.showtoast(d.msg || d.data || '服务器错误,请稍后重试')
      }
    }).catch((e) => {
      utils.hideLoading();
      wx.stopPullDownRefresh();
      var { recordlist } = this.data;
      this.setData({
        moreType: "none",
        statusType: recordlist.length > 0 ? 'none' : 'empty',
        statuserro: recordlist.length > 0 ? false : true,
      })
      utils.showtoast('网络错误，请检查您的网络')
    })
  },
  scrolltolower: function () { //滚动触底加载
    var {
      moreType,
      currentPage,
    } = this.data;
    if (moreType == "showText") {
      var currentPage = currentPage + 1;
      this.setData({
        currentPage,
        moreType: "loading"
      }, () => {
        this.paycost();
      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refresh();
  },
})